package com.huangwenshuo.futurestarschoolmanagement.service.impl;

import com.huangwenshuo.futurestarschoolmanagement.exception.CustomRuntimeException;
import com.huangwenshuo.futurestarschoolmanagement.mapper.ActivityUsersMapper;
import com.huangwenshuo.futurestarschoolmanagement.mapper.UsersMapper;
import com.huangwenshuo.futurestarschoolmanagement.pojo.dto.ActivityDto;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.ActivityUsersPo;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.ActivitysPo;
import com.huangwenshuo.futurestarschoolmanagement.mapper.ActivitysMapper;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.ActivitysVo;
import com.huangwenshuo.futurestarschoolmanagement.service.ActivitysService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangwenshuo
 * @since 2021-02-28
 */
@Service
public class ActivitysServiceImpl extends ServiceImpl<ActivitysMapper, ActivitysPo> implements ActivitysService {

    @Autowired
    private ActivitysMapper activitysMapper ;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private ActivityUsersMapper activityUsersMapper;

    @Override
    public Map<String,Object> getActivitys(ActivityDto.ActivityPage activityPage) {
        Map<String,Object> res = new HashMap<>();
        int page = activityPage.getPage();
        activityPage.setPage((activityPage.getPage()-1)*activityPage.getRows());
        activityPage.setRows(page*activityPage.getRows());
        List<ActivitysVo> activitysVos = activitysMapper.getActivitys(activityPage);
        Integer total = activitysMapper.getActivityByIdTotal(activityPage);
        res.put("acts",activitysVos);
        res.put("total",total);
        return res;
    }

    @Override
    public ActivitysVo getActivityById(ActivityDto.ActivityPage activityPage) {
        Map<String,Object> res = new HashMap<>();
        ActivitysVo activitysVo = activitysMapper.getActivityById(activityPage);
        return activitysVo;
    }

    @Override
    public void updateAct(ActivityDto.UpdateAct updateAct) {
        ActivitysVo activitysVo = activitysMapper.selectAct(updateAct);
        if(activitysVo == null){
            throw new  CustomRuntimeException("该活动不存在！");
        }
        if(activitysVo.getAuthor() != updateAct.getUid()){
            throw new  CustomRuntimeException("您没有编辑的权限！");
        }
        activitysMapper.updateAct(updateAct);
    }

    @Override
    public void addAct(ActivityDto.AddAct addAct) {
        activitysMapper.addAct(addAct);
    }

    @Override
    public void apply(Integer uid, Integer aid) {
            ActivitysVo activitysVo = activitysMapper.selectActByAid(aid);
            if(activitysVo.getAlreadyAddPeople() >= activitysVo.getRestrictPeople()){
                throw new CustomRuntimeException("活动太火爆了，名额已经没了");
            }
        Map<String, String> user = usersMapper.findByUid(uid);
        ActivityUsersPo activityUsersPo = new ActivityUsersPo();
        activityUsersPo.setAid(aid);
        activityUsersPo.setUid(uid);
        activityUsersPo.setAvatar(user.get("avatar"));
        activityUsersPo.setUsername(user.get("username"));
        activityUsersMapper.apply(activityUsersPo);

    }

    @Override
    public Map<String, Object> getActApply(ActivityDto.ActivityPage activityPage) {
        Map<String,Object> res = new HashMap<>();
        int page = activityPage.getPage();
        activityPage.setPage((activityPage.getPage()-1)*activityPage.getRows());
        activityPage.setRows(page*activityPage.getRows());
        List<ActivitysVo> activitysVos = activitysMapper.getActApply(activityPage);
        Integer total = activitysMapper.getActApplyTotal(activityPage);
        res.put("acts",activitysVos);
        res.put("total",total);
        return res;
    }
}
